package com.lianqi.emcpframework.utils;
import java.util.Random;

/**
 * @program emcp-framework
 * @ClassName RandomUtil
 * @description: 随机数生成util类 用于生成随机数，随机密码等
 * @author: sky
 * @create: 2018/11/13 09:29
 */
public class RandomUtil {

    private  static final Random random = new Random();
    private static char[] chars = new char[]{
            'a', 'b', 'c', 'd', 'e', 'f', 'g',
            'h', 'i', 'j', 'k', 'l', 'm', 'n',
            'o', 'p', 'q', 'r', 's', 't',
            'u', 'v', 'w', 'x', 'y', 'z',
            '0', '1', '2', '3', '4', '5', '6',
            '7', '8', '9',
            'A', 'B', 'C', 'D', 'E', 'F', 'G',
            'H', 'I', 'J', 'K', 'L', 'M', 'N',
            'O', 'P', 'Q', 'R', 'S', 'T',
            'U', 'V', 'W', 'X', 'Y', 'Z'};

    public static final int DEFAULT_RANDOM_CHAR = 8;

    public RandomUtil(){
    }

    public static Random getRandom(){
        return random;
    }

    /**
     * 随机生成0-9a-zA-Z组合的随机字符串
     * @param len 字符串长度
     * @return String
     */
    public static String randomChar(int len){
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < len; i ++){
            stringBuilder.append(chars[random.nextInt(chars.length)]);
        }
        return stringBuilder.toString();
    }

    /**
     * 长度为8的随机字符串  可用于随机密码
     * @return
     */
    public static String randomChar(){
        return randomChar(DEFAULT_RANDOM_CHAR);
    }
}
